<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>
<style>
    table tbody {
        display: block;
        height: 195px;
        overflow-y: scroll;
    }

    table thead,
    tbody tr {
        display: table;
        width: 100%;
        table-layout: fixed;
    }

    /*table thead {
    width: calc( 100% - 1em )
}*/
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            成品发货
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">客户</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请选择/扫描客户" id="customerCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectCustomer()"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描/输入发货单号" id="consignment" value=""
                            onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描托码" id="trayinfo" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tm">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码ID</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tmID">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">转移数量</div>
                    <div class="aui-list-item-input">
                        <input type="number" id="qty" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">调入库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请选择/扫描调入库位" id="relWorkCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('2')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">调出库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请选择/扫描调出库位" id="workCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('1')"></i>
                </div>
            </li>
        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
        <div class="table-responsive" id="appStorage" style="overflow-y:auto;height: 13rem;">
            <table id="tab" class="table table-bordered " style="table-layout: fixed;">
                <thead>
                    <tr id="tr1"
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-family: 微软雅黑;font-size: 13px;font-weight: bold;">

                        <td style="width:100px;">箱号</td>
                        <td style="width:120px;;">物料号</td>
                        <td style="width:60px;">批次号</td>
                        <td style="width:60px;">数量</td>
                        <td style="width:80px;">操作结果</td>
                        <td style="width:60px;">操作信息</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml"
                    style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>


                </tbody>
            </table>
        </div>

    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var customerGid = ""; //客户GID
    var customerCode = ""; //客户编码
    var customerName = ""; //客户名称
    var workCellGid = ""; //调出库位GID
    var workCellCode = ""; //调出库位编码
    var workCellName = ""; //调出库位名称
    var relWorkCellGid = ""; //调入库位GID
    var relWorkCellCode = ""; //调入库位编码
    var relWorkCellName = ""; //调入库位名称

    var scanRule = ""; //条码解析规则
    var relworkcell; //菜单路由参数
    var originworkcell; //菜单路由参数

    var arrSave = [];//需要提交的箱信息

    //初始化查询仓库下拉框
    apiready = function () {

        //菜单参数
        var query = api.pageParam.query;
        relworkcell = query.relworkcell
        originworkcell = query.originworkcell
        console.log("目：" + relworkcell);
        console.log("源：" + originworkcell);

        // idSelector = "relWorkCellCode";
        //查询调入库位
        // getWorkCell(relworkcell, idSelector, "2");
        // idSelector = "workCellCode";
        //查询调出库位
        // getWorkCell(originworkcell, idSelector, "1");

        $("#customerCode").focus();
        idSelector = "customerCode";

        //查询条码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：调出库位
            if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询调出库位
                getWorkCell(ret, idSelector, "1");

                //24：调入库位
            } else if (idSelector == 'relWorkCellCode') {

                $("#relWorkCellCode").val(ret);
                //查询调入库位
                getWorkCell(ret, idSelector, "2");

                //5：扫描条码
            } else if (idSelector == 'boxnum') {
                $("#boxnum").val("");
                $("#boxnum").blur();
                tmSplit(ret);
            } else if (idSelector == 'customerCode') {

                $("#customerCode").val(ret);
                //查询客户
                getCustomer(ret, idSelector);
            } else if (idSelector == 'consignment') {
                $("#consignment").val(ret);
            } else if (idSelector == 'trayinfo') {
                $("#trayinfo").val(ret);
                checkoutTrayinfo();
            }
        });
    }

    //7:库位扫描校验
    function getWorkCell(code, inputId, flag) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "out_kj", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: "", //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);

                    //源库位
                    if (flag == "1") {

                        workCellGid = list[0].workCellGid;
                        workCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                        workCellName = list[0].workCellName;
                        document.getElementById(inputId).value = workCellCode;
                        $("#customerCode").focus();
                        idSelector = "customerCode";

                        //调入库位
                    } else if (flag == "2") {

                        relWorkCellGid = list[0].workCellGid;
                        relWorkCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                        relWorkCellName = list[0].workCellName;
                        document.getElementById(inputId).value = relWorkCellCode;
                        $("#workCellCode").focus();
                        idSelector = "workCellCode";
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#" + inputId).focus();
                    $("#" + inputId).select();
                }
            }
        });
    }

    //8：选择库位
    function goSelectWorkCell(workCellFlag) {
        var tm = $("#tm").val();
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "out_kj",
                name: "uwmInterBankTransferCpfh",
                tm: tm,
                workCenterGid: "",
                relWorkCellGid: relWorkCellGid,
                workCellFlag: workCellFlag
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name, workCellFlag) {

        //源库位
        if (workCellFlag == "1") {

            workCellGid = id;
            workCellCode = code;
            workCellName = name;
            document.getElementById('workCellCode').value = code + ":" + name;

            //调入库位
        } else if (workCellFlag == "2") {

            relWorkCellGid = id;
            relWorkCellCode = code;
            relWorkCellName = name;
            document.getElementById('relWorkCellCode').value = code + ":" + name;
            $("#workCellCode").focus();
            var e = "workCellCode";
            idSelector = e;
            getCustomerByCell(relWorkCellCode)
        }
    }

    //客户
    function getCustomerByCell(relWorkCellCode) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getCustomerByCell";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                code: relWorkCellCode,
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log(JSON.stringify(myobj))
                if (myobj.code == '0') {
                    $("#customerCode").val(myobj.customer);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#customerCode").focus();
                    $("#customerCode").select();
                }
            }
        });
    }

    //9；PDA自带扫描回车事件-源库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#workCellCode").val(sm);
            getWorkCell(sm, "workCellCode", "1");
        }
    });

    //10；PDA自带扫描回车事件-调入库位
    $("#relWorkCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#relWorkCellCode").val(sm);
            getWorkCell(sm, "relWorkCellCode", "2");
        }
    });

    //11：选择客户
    function goSelectCustomer() {
        api.openWin({
            name: 'uwmSelectCustomer',
            url: 'uwmSelectCustomer.html',
            pageParam: {
                name: "uwmInterBankTransferCpfh",
            }
        });
    }

    //客户选择后回调函数
    function setCustomer(id, code, name) {
        customerGid = id;
        customerCode = code;
        customerName = name;
        //$("#workCenterCode").attr("value",code+":"+name);
        document.getElementById('customerCode').value = code + ":" + name;
        $("#customerCode").focus();
        idSelector = "customerCode";
    }

    //客户扫描校验
    function getCustomer(code, inputId) {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem('un');
        var url = serverUrl + "/app/getCustomer";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                customerCode: code,
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    customerGid = list[0].customerGid;
                    customerCode = list[0].customerCode + ":" + list[0].customerName;
                    customerName = list[0].customerName;
                    document.getElementById(inputId).value = customerCode;
                    $("#customerCode").focus();
                    idSelector = "customerCode";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#customerCode").focus();
                    $("#customerCode").select();
                }
            }
        });
    }

    //保存按钮
    function save() {

        //物料号
        // var mrlCode = $("#mrlCode").val();
        // //批次号
        // var lotCode = $("#lotCode").val();
        // //转移数量数量
        // var qty = $("#qty").val();
        // //条码
        // var tm = $("#tm").val();
        // //箱号
        // var boxnum = $("#boxnum").val();
        // //条码ID
        // var tmID = $("#tmID").val();
        //发货单号

        var consignment = $("#consignment").val();

        //客户非空校验
        if (customerGid == "" || customerGid == null || customerGid == undefined) {
            api.toast({
                msg: '请选择/扫描客户',
                duration: 3000
            });
            return;
        }

        //发货单号非空校验
        // if (consignment == "" || consignment == null || consignment == undefined) {
        //     api.toast({
        //         msg: '请输入/扫描发货单号',
        //         duration: 3000
        //     });
        //     return;
        // }

        //调入库位非空校验
        if (relWorkCellGid == "" || relWorkCellGid == null || relWorkCellGid == undefined) {
            api.toast({
                msg: '请选择/扫描 调入库位',
                duration: 3000
            });
            return;
        }

        //箱码非空校验
        // if (boxnum == "" || boxnum == null || boxnum == undefined) {
        //     api.toast({
        //         msg: '箱号不能为空',
        //         duration: 3000
        //     });
        //     return;
        // }
        //
        // //物料非空校验
        // if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
        //     api.toast({
        //         msg: '物料号不能为空',
        //         duration: 3000
        //     });
        //     return;
        // }
        //
        // //批次号非空校验
        // if (lotCode == "" || lotCode == null || lotCode == undefined) {
        //     api.toast({
        //         msg: '批次号不能为空',
        //         duration: 3000
        //     });
        //     return;
        // }
        //
        // //转移数量非空校验
        // if (qty == "" || qty == null || qty == undefined) {
        //     api.toast({
        //         msg: '请输入转移数量',
        //         duration: 3000
        //     });
        //     return;
        // } else {
        //     if (parseFloat(qty) <= 0) {
        //         api.toast({
        //             msg: '转移数量必须大于0',
        //             duration: 3000
        //         });
        //         return;
        //     }
        // }



        //源仓库和目标仓库不能一样
        if (workCellGid != null && workCellGid != "" && workCellGid == relWorkCellGid) {
            api.toast({
                msg: '源库位和调入库位不能一样',
                duration: 3000
            });
            return;
        }


        if (arrSave.length == 0) {
            api.toast({
                msg: '没有需要保存的物料信息',
                duration: 3000
            });
            return;
        }

        console.log("arrSave222:" + JSON.stringify(arrSave));

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmInterBankTransferSaveCpfh";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                customerGid: customerGid, // 客户
                consignment: consignment, // 发货单号
                relWorkCellGid: relWorkCellGid, // 调入库位
                workCellGid: workCellGid, //源库位编码
                list: JSON.stringify(arrSave),
                // mrlCode: mrlCode, //物料号
                // lotCode: lotCode, //批次号
                // qty: qty, //转移数量
                // tm: tm, //条码
                // boxnum: boxnum, //箱号
                // tmID: tmID, //条码ID
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log("返回来啦:" + data);
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    // api.toast({
                    //     msg: "保存成功",
                    //     duration: 3000
                    // }); //清空表单
                    arrSave = [];
                    $("#customerCode").val("");
                    $("#consignment").val("");
                    $("#tm").val("");
                    $("#tmID").val("");
                    $("#boxnum").val("");
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    //回显保存结果
                    console.log("list:" + JSON.stringify(myobj.list));
                    var list = myobj.list;
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr >";
                        html += "<td name='boxnum' style='width:100px;'>" + list[i].boxnum + "</td>";
                        html += "<td name='mrlCode' style='width:120px;'>" + list[i].mrlCode + "</td>";
                        html += "<td name='lotCode' style='width:60px;'>" + list[i].lotCode + "</td>";
                        html += "<td name='qty' style='width:60px;'>" + list[i].qty + "</td>";
                        if (list[i].resultCode == 'S') {
                            html += "<td name='resultCode' style='background-color:green;width:80px;'>" + list[i].resultCode + "</td>";
                            html += "<td name='resultMsg' style='background-color:green;width:60px;'>" + list[i].resultMsg + "</td>";
                        } else if (list[i].resultCode == 'E') {
                            html += "<td name='resultCode' style='background-color:red;width:80px;'>" + list[i].resultCode + "</td>";
                            html += "<td name='resultMsg' style='background-color:red;width:60px;'>" + list[i].resultMsg + "</td>";
                        }
                        html += "</tr>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);




                    $("#message").html(myobj.message);
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                } else if (myobj.code == '1') {
                    $("#boxnum").val("");
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }





    //扫描之后条码解析
    function tmSplit(sm) {
        // $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }
    //自动保存
    function saveAuto(tm, mrlCode, lotCode, qty, boxnum, tmID) {



        //物料号
        // var mrlCode = $("#mrlCode").val();
        // //批次号
        // var lotCode = $("#lotCode").val();
        // //转移数量数量
        // var qty = $("#qty").val();
        // //条码
        // var tm = $("#tm").val();
        // //箱号
        // var boxnum = $("#boxnum").val();
        // //tmID
        // var tmID = $("#tmID").val();







        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '箱号不能为空',
                duration: 3000
            });
            return;
        }

        //物料非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '物料号不能为空',
                duration: 3000
            });
            return;
        }

        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            api.toast({
                msg: '批次号不能为空',
                duration: 3000
            });
            return;
        }

        //转移数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            api.toast({
                msg: '请输入转移数量',
                duration: 3000
            });
            return;
        } else {
            if (parseFloat(qty) <= 0) {
                api.toast({
                    msg: '转移数量必须大于0',
                    duration: 3000
                });
                return;
            }
        }
        //判断箱号是否重复
        for (var i = 0; i < arrSave.length; i++) {
            if (boxnum == arrSave[i].boxnum) {
                api.toast({
                    msg: '箱号已存在！',
                    duration: 3000
                });
                $("#boxnum").val("");
                $("#boxnum").select();
                idSelector = "boxnum";
                return;
            }
        }
        //校验扫码物料属于选择客户
        checkout(tm, mrlCode, lotCode, qty, boxnum, tmID);
        $("#boxnum").focus();
        $("#boxnum").select();

        // var model={
        //   mrlCode: mrlCode, //物料号
        //   lotCode: lotCode, //批次号
        //   qty: qty, //转移数量
        //   tm: tm, //条码
        //   boxnum: boxnum, //箱号
        //   tmID: tmID //条码ID
        // }
        // arrSave.push(model);
        // console.log(arrSave)
        // console.log()
        //freshTable();
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        var mrlCode = "";
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            $("#mrlCode").val(arr[1]);
            mrlCode = arr[1];
        }
        var qty = "";
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            $("#qty").val(arr[2]);
            qty = arr[2];
        }
        var lotCode = "";
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            $("#lotCode").val(arr[3]);
            lotCode = arr[3];
        }
        var tmID = "";
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            $("#tmID").val(arr[4]);
            tmID = arr[4];
        }
        var boxnum = "";
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            // $("#boxnum").val(arr[9]);
            boxnum = arr[9];
        }
        //自动保存
        saveAuto(sm, mrlCode, lotCode, qty, boxnum, tmID);

    }

    //校验
    function checkout(tm, mrlCode, lotCode, qty, boxnum, tmID) {
        // var mrlCode = $("#mrlCode").val();//物料号
        // var lotCode = $("#lotCode").val();//
        // var qty = $("#qty").val();//
        // var tm = $("#tm").val();//
        // var boxnum = $("#boxnum").val();//
        // var tmID = $("#tmID").val();//
        var customerCode = $("#customerCode").val();//客户编码
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getCheckOut";
        var data = {
            values: {
                un: un,
                mrlCode: mrlCode,
                customerCode: customerCode
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                console.log(myobj.code);
                if (myobj.code == '0') {

                    if (tm.indexOf("," + boxnum) != -1) {
                        var model = {
                            mrlCode: mrlCode, //物料号
                            lotCode: lotCode, //批次号
                            qty: qty, //转移数量
                            tm: tm, //条码
                            boxnum: boxnum, //箱号
                            tmID: tmID //条码ID
                        }
                        arrSave.push(model);
                        $("#boxnum").val("");
                        $("#boxnum").select();
                        idSelector = "boxnum";
                        freshTable();
                    } else {
                        api.toast({
                            msg: "条码扫描异常，请重新扫描",
                            duration: 2000
                        });
                    }


                    //自动保存
                    //                   saveAuto();

                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector = "boxnum";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#boxnum").val("");
                $("#boxnum").select();
                idSelector = "boxnum";
                api.hideProgress();
                alert(JSON.stringify(err));

            }
        });
    }

    //删除备货箱
    function delDetail(boxnum) {
        api.confirm({
            title: '提示',
            msg: "确认撤销箱号 【" + boxnum + "】物料？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                var newArr = [];
                for (var i = 0; i < arrSave.length; i++) {
                    if (boxnum != arrSave[i].boxnum) {
                        newArr.push(arrSave[i]);
                    }
                }
                arrSave = newArr;
                freshTable();
            }
        });
    }
    function freshTable() {
        var html = "";
        for (var i = 0; i < arrSave.length; i++) {
            // style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;' onclick='delDetail(\"" + arrSave[i].boxnum + "\")'
            html += "<tr >";
             html += "<td name='boxnum' style='width:100px;'>" + arrSave[i].boxnum + "</td>";
            //html += "<td name='boxnum' style='width:100px;text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;' onclick='delDetail(" + arrSave[i].boxnum + ")' " + "</td>";
            html += "<td name='mrlCode' style='width:120px;'>" + arrSave[i].mrlCode + "</td>";
            html += "<td name='lotCode' style='width:60px;'>" + arrSave[i].lotCode + "</td>";
            html += "<td name='qty' style='width:60px;'>" + arrSave[i].qty + "</td>";
            html += "<td name='resultCode' style='width:80px;'></td>";
            html += "<td name='resultMsg' style='width:60px;'></td>";
            html += "</tr>";
        }

        console.log("html:" + JSON.stringify(html));
        $api.html($api.byId("tbodyHtml"), html);
    }


    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    console.log("analyss:" + JSON.stringify(ret));
                    $("#boxnum").val("");
                    //物料号
                    var mrlCode = map["mrlCode"];
                    //批次号
                    var lotCode = map["lotCode"];
                    //转移数量数量
                    var qty = map["qty"];
                    //条码
                    var tm = map["tm"];
                    //箱号
                    var boxnum = map["boxnum"];
                    //tmID
                    var tmID = map["tmID"];
                    //自动保存
                    saveAuto(tm, mrlCode, lotCode, qty, boxnum, tmID);
                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        if (!clickThrottle()) {
            $("#boxnum").val("");
            return;
        }
        var keycode = e.keyCode;
        var tm = $(this).val();
        $("#boxnum").val("");
        if (keycode == '13') {
            //$("#boxnum").select();
            //调用条码解析+保存事件
            console.log("tm11:" + tm);
            $("#boxnum").blur();
            console.log("tm22:" + tm);
            tmSplit(tm);
        }
    });
    //托码监听回车事件
    $("#trayinfo").on('keypress', function (e) { //#keyword为input文本框
         if (!clickThrottle()) {
            $("#boxnum").val("");
            return;
        }
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#trayinfo").val(tm);
            $("#trayinfo").select();
            //调用条码解析+保存事件
            checkoutTrayinfo();
        }
    });
    function checkoutTrayinfo() {
        var trayinfo = $("#trayinfo").val();//客户编码
        var customerCode = $("#customerCode").val();//客户编码
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getCheckOutTrayinfo";
        var data = {
            values: {
                un: un,
                trayinfo: trayinfo,
                customerCode: customerCode
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                console.log(myobj.code);
                if (myobj.code == '0') {
                    var map = myobj.list;
                    for (var p in map) {
                        var model = {
                            mrlCode: map[p].mrlCode, //物料号
                            lotCode: map[p].lotCode, //批次号
                            qty: map[p].qty, //转移数量
                            tm: map[p].tm, //条码
                            boxnum: map[p].boxnum, //箱号
                            tmID: map[p].tmID //条码ID
                        }
                        //判断箱号是否重复
                        for (var i = 0; i < arrSave.length; i++) {
                            if (map[p].boxnum == arrSave[i].boxnum) {
                                api.toast({
                                    msg: '箱号已存在！',
                                    duration: 3000
                                });

                                return;
                            }
                        }
                        arrSave.push(model);
                    }

                    freshTable();
                    //自动保存
                    //                   saveAuto();

                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>